// pages/member/article/noticeByCategory/noticeByCategory.js
var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    category_id: '',
    articleList: [],

    //more
    isLoadMore: true,
    page: 1,
    total_page: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      category_id: options.category_id
    });

    this._getData();
  },
  _getData: function (success) {
    var that = this;
    // 初次获取数据
    app._getNetWork({
      url: app.getNetAddresss("plugin.article.api.article.get-articles"),
      data: {
        category_id: that.data.category_id,
        page: 1
      },
      success: function (res) {
        if (res.data.result == 1) {
          let _data = res.data.data;
          that.setData({
            total_page: _data.articles.last_page,
            articleList: _data.articles.data
          });
          // 设置页面标题
          wx.setNavigationBarTitle({
            title: _data.title
          });
          if (success != undefined) {
            success(res);
          }
        } else {
          console.error(res);
          this.setData({
            page: this.data.page - 1,
            isLoadMore: false
          });
        }
      },
      fail: function (res) {
        console.error(res);
      }
    });
  },

  _getMoreData() {
    var that = this;
    let urlStr = app.getNetAddresss("plugin.article.api.article.get-articles") + '&category_id=' + this.data.category_id;
    if (this.data.page >= this.data.total_page) {
      that.setData({
        isLoadMore: false
      });
      return;
    } else {
      that.setData({
        page: this.data.page + 1
      });
      urlStr += "&page=" + that.data.page;
      app._getNetWork({
        url: urlStr,
        success: function (res) {
          if (res.data.result == 1) {
            let _data = res.data.data;
            that.setData({
              isLoadMore: true,
              articleList: that.data.articleList.concat(_data.articles.data)
            });
          } else {
            that.setData({
              page: that.data.page - 1,
              isLoadMore: false
            });
            return;
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
    }
  },

  toArticle: function (e) {
    let itemid = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/packageA/member/article/articleContent/articleContent?article_id=' + itemid
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      isLoadMore: true,
      page: 1,
      total_page: 0
    });
    this._getData();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.isLoadMore) {
      this._getMoreData();
    } else {
      console.log('没有更多数据');
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  onShareTimeline: function () { },
});